package com.sxhalo.fsms.controller.api;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.service.IDeviceAlarmRecordService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/api")
public class DeviceAlarmRecordController extends BaseController {

	@Autowired
	private IDeviceAlarmRecordService alarmRecordService;



	@ResponseBody
	@RequestMapping(value = "/alarm/analysis", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findAlarmAnalysis(HttpServletRequest req) {
		Map<String,Integer> aramNum=alarmRecordService.getAramNumStatistics();
		Map<String,Object> alarmData=findAlarmAnalysisByType(1);
		alarmData.put("aramNum",aramNum);
		return ApiResult.retOK(req,alarmData);
	}



	@ResponseBody
	@RequestMapping(value = "/alarm/analysisByType", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	public ApiResult findAlarmAnalysisByType(Integer typeId,HttpServletRequest req) {
		return ApiResult.retOK(req,findAlarmAnalysisByType(typeId));
	}



	public Map<String,Object> findAlarmAnalysisByType(Integer typeId) {
		Map<String,Object> alarmData=new HashMap<String,Object>();
		typeId=typeId==null?1:typeId;

		Date d = new Date();
		String time="";
		 if(typeId==3){
			 SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			 time = sdf.format(d)+"-00-00 00:00:00";
		 }else if(typeId==2){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
			time = sdf.format(d)+"-00 00:00:00";

		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			time = sdf.format(d);

		}


		List<Map<String,Integer>>  typenum= alarmRecordService.getAramTypeNum(typeId==1?"alarm_time >  CURDATE()":typeId==2?" DATE_FORMAT(alarm_time, '%Y-%m')  >=  DATE_FORMAT(CURDATE(), '%Y-%m')": " YEAR(alarm_time) >=  YEAR(CURDATE())");
		List<Map<String,Integer>>  trendnum =alarmRecordService.GetalarmTrendStatistics(typeId);
		List<Map<String,Object>>  areanum =alarmRecordService.getAramTypeNumByArea(time);
//		Object[] trendTime=new Object[trendnum.size()];
//		Object[] trendData=new Object[trendnum.size()];
//		for(int  i=0;i<trendnum.size();i++){
//			trendTime[i]=trendnum.get(i).get("time");
//			trendData[i]=trendnum.get(i).get("num");
//		}
		alarmData.put("typenum",typenum);
		alarmData.put("trendnum",trendnum);
//		alarmData.put("trendTime",trendTime);
//		alarmData.put("trendData",trendData);
		alarmData.put("areaNum",areanum);
		return alarmData;

	}


}
